package castledefense.gui;

import twl.global.libgdx.GlobalTWL;
import castledefense.data.GameData;
import castledefense.game.screens.MainGameScreen;
import de.matthiasmann.twl.Widget;

public class GUIMainGame {

	private BuildingPanel buildingPanel;
	private UpgradePanel upgradePanel;
	private MinimapPanel minimapPanel;
	private SpellPanel spellPanel;
	private LevelPanel levelPanel;

	private MainGameScreen state;
	private GlobalTWL twl;
	private Widget rootPane;

	private boolean dragging = false;

	public GUIMainGame(MainGameScreen state, GlobalTWL twl) {
		this.state = state;
		this.twl = twl;
		rootPane = new Widget();
		rootPane.setTheme("");
		twl.getGUI().getRootPane().add(rootPane);

		initGUI();
	}

	public void initGUI() {
		rootPane.setSize(GameData.SCREENSIZEX, GameData.SCREENSIZEY);

		Widget tmpPanel = new Widget();
		tmpPanel.setTheme("panelTheme");
		tmpPanel.setSize(179, 179);
		tmpPanel.setPosition(5, 5);
		rootPane.add(tmpPanel);
		minimapPanel = new MinimapPanel(this, state);
		minimapPanel.setPosition(13, 13);
		minimapPanel.setSize(MainGameScreen.mapSizeX * MinimapPanel.cellSize,
				MainGameScreen.mapSizeY * MinimapPanel.cellSize);
		tmpPanel.add(minimapPanel);

		buildingPanel = new BuildingPanel(this);
		buildingPanel.setPosition(5, 189);
		buildingPanel.setSize(179, 150);
		rootPane.add(buildingPanel);

		upgradePanel = new UpgradePanel(this);
		upgradePanel.setPosition(5, 344);
		int tmp = GameData.SCREENSIZEY - 5 - 344;
		if (tmp < 0)
			tmp = 0;
		upgradePanel.setSize(179, tmp);
		rootPane.add(upgradePanel);

		spellPanel = new SpellPanel(this, state.getActivatedAbilities());
		// spellPanel.setSize(295, 34);
		spellPanel.setSize(307, 40);
		spellPanel.setPosition(GameData.SCREENSIZEX / 2 - spellPanel.getWidth()
				/ 2, GameData.SCREENSIZEY - spellPanel.getHeight() - 5);
		rootPane.add(spellPanel);

		levelPanel = new LevelPanel(this);
		levelPanel.setSize(327, 40);
		levelPanel.setPosition(GameData.SCREENSIZEX / 2 - spellPanel.getWidth()
				/ 2, 5);
		rootPane.add(levelPanel);

		rootPane.getGUI().setTooltipDelay(500);
	}

	public void render() {
		twl.render();
	}

	public GlobalTWL getTWL() {
		return twl;
	}

	public MainGameScreen getState() {
		return state;
	}

	public Widget getRootPane() {
		return rootPane;
	}

	public BuildingPanel getBuildingPanel() {
		return buildingPanel;
	}

	public UpgradePanel getUpgradePanel() {
		return upgradePanel;
	}

	public MinimapPanel getMinimapPanel() {
		return minimapPanel;
	}

	public SpellPanel getSpellPanel() {
		return spellPanel;
	}

	public LevelPanel getLevelPanel() {
		return levelPanel;
	}

	public void setDragging(boolean value) {
		dragging = value;
	}

	public boolean isInsideGui(int x, int y) {
		boolean result = false;
		result |= dragging;
		result |= buildingPanel.isInside(x, y);
		result |= (upgradePanel.isInside(x, y) && upgradePanel.isVisible());
		result |= minimapPanel.isInside(x, y);
		result |= spellPanel.isInside(x, y);
		return result;
	}

}
